package com.leapp.sk.app
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.StrictMode
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lkbasecomponent.http.lib.wrap.HttpLogginLevel
import cc.vv.lkimcomponent.lkim.inter.LKIMOperateInter
import cc.vv.lklibrary.log.LogOperate
import cn.jpush.android.api.JPushInterface
import com.example.baselibrary.db.BTDBHelper
import com.example.im.IMInit
import com.example.BaseNewApplication
import com.leapp.sk.ui.activity.login.LoginActivity


/**
 * SK基础Application
 * @author  chaohaipeng  on 2018/7/2.
 */
class SKApplication : BaseNewApplication() {

    private object mHolder {
        val instance = SKApplication()
    }
    companion object {
        var  mApplication: SKApplication?=null

        fun getInstance(): SKApplication? {
            return mApplication
        }
    }
    override fun onCreate() {
        super.onCreate()
        mApplication=this
        JPushInterface.setDebugMode(true) // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this)
        //
        BTDBHelper.initInstance(getInstance())
        //debug打印或不打印
        LogOperate.setIsDebug(true)
        IMInit.getInstance().init(getInstance())
            LKHttp.init().initLogLevel(HttpLogginLevel.Level.EASY)
       //LKHttp.init().initLogLevel(HttpLogginLevel.Level.NONE)
        //设置默认超时时间
        LKHttp.init().initTimeout(Settings(5000, (60 * 1000).toLong(), (60 * 1000).toLong()))
        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler())

    }
    fun getApplicationHandler(): Handler? {
        return mHandler
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)

    }

//    /**
//     * 遍历所有的Activity并finish
//     */
//    fun exitToLogin() {
//        val intent = Intent(this, LoginActivity::class.java)
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
//        this.startActivity(intent)
//
//    }

    /**
     * 全局异常捕获回调
     *
     * @author Administrator
     */
    class UncaughtExceptionHandler : Thread.UncaughtExceptionHandler {

        override fun uncaughtException(thread: Thread, ex: Throwable) {
            // TODO Auto-generated method stub

            // 干掉当前页面
            android.os.Process.killProcess(android.os.Process.myPid())

        }
    }
}